const http = require("http");
const fs = require("fs");

const server = http.createServer();

var wwwDir = "D:/app/www";

server.on("request", (req, res) => {
    const url = req.url;
    fs.readFile("./template.html", (err, data) => {
        if (err) {
            return res.end("404 Not Found")
        }

        fs.readdir(wwwDir, (err, files) => {
            if (err) {
                return res.end("Can not found www dir")
            }
            let content = '';
            files.forEach(item => {
                content += ` 
                     <tr>
                        <td data-value="img/">
                            <a class="icon dir" href="/D:/app/www/img/">${item}</a>
                        </td>
                            <td class="detailsColumn" data-value="0"></td>
                            <td class="detailsColumn" data-value="1561796674">2019/6/29 下午4:24:34</td>
                        </tr>
                            `;

            });

            data = data.toString();
            data = data.replace("molifei", content);
            res.end(data)
        });
    })
});

server.listen(2500, () => {
    console.log("this server is running at http://127.0.0.1:2500")
});
